#pragma once

#include <iostream>
#include <sys/epoll.h>
#include "log.hpp"

static const int defaultfd = -1;
static const int size = 128;

enum
{
    EPOLL_CREATE_ERR
};

class Epoller
{
public:
    Epoller() : _epfd(defaultfd) {}
    ~Epoller()
    {
        if (_epfd != defaultfd)
            close(_epfd);
    }

    void Create()
    {
        _epfd = epoll_create(size);
        if (_epfd < 0)
        {
            logMessage(FATAL, "epoll_create error,code :%d  codeString: %s", errno, strerror(errno));
            exit(EPOLL_CREATE_ERR);
        }
    }

    bool AddEvent(int sock, uint32_t events)
    {
        struct epoll_event ev;
        ev.data.fd = sock;
        ev.events = events;
        int n = epoll_ctl(_epfd, EPOLL_CTL_ADD, sock, &ev);
        return n == 0;
    }

    int Wait(struct epoll_event revs[], int num, int timeout)
    {
        int n = epoll_wait(_epfd, revs, num, timeout);
        return n;
    }

    bool Control(int sock ,uint32_t event,int action)
    {
        int n=0;
        if(action==EPOLL_CTL_MOD)
        {
            struct epoll_event ev;
            ev.events=event;
            ev.data.fd=sock;
            n=epoll_ctl(_epfd,action,sock,&ev);
        }
        else if(action==EPOLL_CTL_DEL)
        {
            n=epoll_ctl(_epfd,action,sock,nullptr);
        }
        else n=-1;
        return n==0;
    }

    void Close()
    {
        if (_epfd != defaultfd)
            close(_epfd);
        _epfd = defaultfd;
    }

private:
    int _epfd;
};